<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.query" placeholder="" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <data-table
      ref="datatable"
      title="用户管理"
      row-key="id"
      url="/user/list"
      highlight-current-row
      style="width: 100%;">
      <el-table-column label="序号" prop="ID" align="center" width="80" sortable>
        <template slot-scope="scope">
          <span>{{ scope.row.ID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" prop="UserName" min-width="80px" sortable/>
      <el-table-column label="姓名" prop="RealName" min-width="80px" sortable/>

      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot="header" slot-scope="scope">
          <el-button v-if="canAdd" type="primary" size="mini" @click="handleCreate">添加</el-button>
          <template v-else>{{ scope.column.label }}</template>
        </template>
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <confirm-poptip size="mini" type="danger" title="确定删除？" @on-ok="handleDelete(scope.row)">删除</confirm-poptip>
        </template>
      </el-table-column>
    </data-table>

    <el-dialog v-el-drag-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="400px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" >

        <el-form-item label="用户名" prop="UserName">
          <el-input v-model="temp.UserName" :max-length="30" :disabled="!isNew"/>
        </el-form-item>
        <el-form-item label="姓名" prop="RealName">
          <el-input v-model="temp.RealName" :max-length="20"/>
        </el-form-item>
        <el-form-item label="密码" prop="Password">
          <el-input v-model="temp.Password" :max-length="30"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="isNew?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, remove } from '@/api/user'
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'
import elDragDialog from '@/directive/el-dragDialog'

export default {
  name: 'UserManage',
  directives: { waves, elDragDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      canAdd: true,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+ID',
        query: null
      },
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      defaultForm: {
        ID: undefined,
        UserName: undefined,
        RealName: undefined,
        Password: undefined
      },
      temp: {
        ID: undefined,
        UserName: undefined,
        RealName: undefined,
        Password: undefined
      },
      dialogFormVisible: false,
      isNew: false,
      pvData: [],
      rules: {
        UserName: [{ required: true, message: '不能为空', trigger: 'change' }],
        RealName: [{ required: true, message: '不能为空', trigger: 'change' }],
        Password: [{ required: false, message: '不能为空', trigger: 'change' }]
      },
      downloadLoading: false
    }
  },
  computed: {
    dialogFormTitle() {
      return this.isNew ? '新建' : '编辑'
    }
  },
  created() {
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = Object.assign({}, this.defaultForm)
    },
    handleCreate() {
      this.resetTemp()
      this.isNew = true
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.temp).then(res => {
            console.log(res)
            this.$notify({
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.datatable.addData(res.data)
          })
        }
      })
    },
    handleUpdate(row) {
      this.isNew = false
      this.dialogFormVisible = true
      this.temp = Object.assign({}, row)
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          update(this.temp).then(res => {
            this.$notify({
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.datatable.updateData(res.data)
          })
        }
      })
    },
    handleDelete(row) {
      remove(row.ID).then(() => {
        this.$notify({
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.$refs.datatable.deleteData(row)
      })
    }
  }
}
</script>
